package com.gavin.demo4fresco.adapters;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.common.logging.FLog;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.gavin.demo4fresco.R;
import com.gavin.demo4fresco.model.entities.PictureObj;

import java.util.List;

/**
 * Created by Gavin on 16/2/11.
 */
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
    private Context mContext;
    private String[] mUrls;
    private List<PictureObj> objs;

    /**
     * 设置监听事件
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public MainAdapter(Context context,String[] urls) {
        this.mContext = context;
        this.mUrls = urls;
    }

    public MainAdapter(Context context,List<PictureObj> objs) {
        this.mContext = context;
        this.objs = objs;
    }

    /**
     * 使用一个 ControllerListener 实现事件的监听
     */
    private ControllerListener listener = new BaseControllerListener<ImageInfo>(){
        /**
         * 成功时触发
         * @param id
         * @param imageInfo
         * @param animatable
         */
        @Override
        public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
            if (imageInfo == null) {
                return;
            }
            QualityInfo qualityInfo = imageInfo.getQualityInfo();
            FLog.d("Final image received! " +
                            "Size %d x %d",
                    "Quality level %d, good enough: %s, full quality: %s",
                    imageInfo.getWidth(),
                    imageInfo.getHeight(),
                    qualityInfo.getQuality(),
                    qualityInfo.isOfGoodEnoughQuality(),
                    qualityInfo.isOfFullQuality());
        }

        /**
         * 失败时触发
         * @param id
         * @param throwable
         */
        @Override
        public void onFailure(String id, Throwable throwable) {
            FLog.e(getClass(), throwable, "Error loading %s", id);
        }

        /**
         * 如果允许呈现渐进式 JPEG，同时图片也是渐进式图片，onIntermediateImageSet 会在每个扫描被解码后回调。
         * @param id
         * @param imageInfo
         */
        @Override
        public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
            FLog.d("","Intermediate image received");
        }
    };

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MainViewHolder viewHolder = new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.main_item_content_recycleview, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MainViewHolder holder, final int position) {
        // 设置进度条,可以自定义进度条,具体详情见 README.md
        GenericDraweeHierarchy hierarchy = holder.draweeView.getHierarchy();
        // 这样，在 Drawee 的底部就会有一个深蓝色的矩形进度条
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        /**
         * 直接调用 setImageURI 的用法
         */
        // holder.draweeView.setImageURI(Uri.parse(objs.get(position).getUrl()));
        /**
         * 使用 DraweeController
         */
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setControllerListener(listener)
                .setUri(Uri.parse(objs.get(position).getUrl()))
                .setTapToRetryEnabled(true)
                .setOldController(holder.draweeView.getController())
                .build();
        holder.draweeView.setController(controller);

        holder.mTextView.setText(objs.get(position).getUrl());

        // 设置监听事件
        if (null != mOnItemClickLitener) {
            holder.mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.mTextView,pos);
                }
            });
            holder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.mTextView,pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return objs.size();
    }

    public void addData(int position) {
        PictureObj obj = new PictureObj();
        obj.setDesc("Insert Desc");
        obj.setUrl("http://img.kutoo8.com//upload/image/78018037/201305280911_960x540.jpg");
        objs.add(position,obj);
        notifyItemInserted(position);
    }

    public void removeData(int position) {
        objs.remove(position);
        notifyItemRemoved(position);
    }

    class MainViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView draweeView;
        TextView mTextView;

        public MainViewHolder(View itemView) {
            super(itemView);
            draweeView = (SimpleDraweeView) itemView.findViewById(R.id.main_item_content_draweeview);
            mTextView = (TextView) itemView.findViewById(R.id.main_item_content_textview);
        }
    }
}
